<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\SiteConfigService;
use apps\marriage\core\service\VipLevelOrderService;
use think\Exception;
use think\Request;

class VipLevelOrderController extends BaseMarriageMpController
{
    /**
     * @var VipLevelOrderService
     */
    private $vipLevelOrderService;

    /**
     * @var SiteConfigService
     */
    private $siteConfigService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->vipLevelOrderService = service('VipLevelOrder', SERVICE_NAMESPACE);
        $this->siteConfigService = service('SiteConfig', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function create(Request $request)
    {
        $fromuid = $request->param('fromuid', 0, 'intval');
        $vipLevel = $request->param('vip_level', 0, 'intval');

        if($fromuid == 0) {
            $fromuid = $this->user['fromuid'];
        } else if($fromuid > 0 && $fromuid == $this->user['id']) {
            $fromuid = 0;
        }

        $site = config('site');
        $vipLevels = $site['vip_levels'];
        if(!isset($vipLevels[$vipLevel])) {
            throw new Exception('参数错误');
        }
        $orderMoney = floatval($vipLevels[$vipLevel]['sale_price']);

        $order = [
            'user_id' => $this->user['id'],
            'fromuid' => $fromuid,
            'order_money' => $orderMoney,
            'vip_level' => $vipLevel
        ];
        $orderSn = $this->vipLevelOrderService->createOrder($order);
        return $this->success(['order_sn' => $orderSn]);
    }
}